package com.yanqu.road.server.cmd.banquet.cross.goodbanquet;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.banquet.BanquetInfo;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.pb.banquet.CrossBanquetProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.banquet.BanquetConfigMgr;
import com.yanqu.road.server.manger.banquet.CrossBanquetMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;

/**
 * @Description
 * @Author cwq
 * @Data 2021/11/27 10:27
 */
@Cmd(code = Protocol.S_CROSS_GOOD_BANQUET_OPEN, desc = "开启百福宴")
public class GoodBanquetOpenCmd extends AbstractCommand {

    int activityId ;
    int type ;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        CrossBanquetProto.OpenCrossGoodBanquetReq req = CrossBanquetProto.OpenCrossGoodBanquetReq.parseFrom(packet.getBytes());
        activityId = req.getActivityId();
        type = req.getType();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        BanquetInfo banquetInfo = BanquetConfigMgr.getBanquetInfo(type);
        if(banquetInfo == null || !BanquetConfigMgr.isCrossGoodBanquet(type)){
            ret = GameErrorCode.E_BANQUET_INFO_ERROR;
            return;
        }
        // 检查 活动是否开启
        if(!CrossBanquetMgr.canOpenGoodBanquet(activityId)){
            ret = GameErrorCode.E_BANQUET_ACTIVITY_NO_IN_TIME;
            return;
        }
        ActivityInfo activityInfo = NormalActivityMgr.getActivityInfo(activityId);
        if(activityInfo.getServerIdList().size() <= 2){
            ret = GameErrorCode.E_BANQUET_ACTIVITY_SERVER_NUM_NO_ENOUGH;
            return;
        }
        Property property = PropertyHelper.parseStringToProperty(banquetInfo.getOpenConsume());
        if(!player.getModule(CurrencyModule.class).currencyIsEnough(property)){
            ret = GameErrorCode.E_BAG_ITEM_NO_ENOUGH;
            return;
        }
        // 预扣除
        player.getModule(PreReduceModule.class).preReduce(property);

        CrossBanquetProto.GoodBanquetOpenToCrossReqMsg.Builder toCrossMsg = CrossBanquetProto.GoodBanquetOpenToCrossReqMsg.newBuilder();
        toCrossMsg.setActivityId(activityId);
        toCrossMsg.setType(type);
        toCrossMsg.setPlayerBaseData(PlayerBasePb.parsePlayerBaseTempMsg(UserMgr.parseUserBaseInfo(player.getUserInfo(), GameServer.getInstance().getServerId())));
        player.sendPacket(Protocol.C2_GOOD_BANQUET_OPEN, toCrossMsg);
    }

    @Override
    public void sendData(GamePlayer player) {
        if(ret != 0){
            CrossBanquetProto.OpenCrossGoodBanquetResp.Builder respMsg = CrossBanquetProto.OpenCrossGoodBanquetResp.newBuilder();
            respMsg.setRet(ret);
            player.sendPacket(Protocol.U_CROSS_GOOD_BANQUET_OPEN, respMsg);
        }
    }
}
